<?php

namespace TpAmis\Traits;

use TpAmis\Renderers\BaseApi;

trait BaseApiTrait
{
    /**
     * 删除接口
     * @param string $thinkUrl thinkphp路由地址
     * @param array $query 请求参数
     * @param array $data  请求数据
     * @param mixed $type 提交类型：json   form-data    form
     * @return BaseApi
     */
    public function BaseApiDelete(string $thinkUrl = '', array $query = [], $data = [], $type = 'form'): BaseApi
    {
        return $this->amisBaseApi($thinkUrl, 'delete', $query, $data, $type);
    }
    /**
     * 修改接口
     * @param string $thinkUrl thinkphp路由地址
     * @param array $query 请求参数
     * @param array $data  请求数据
     * @param mixed $type 提交类型：json   form-data    form
     * @return BaseApi
     */
    public function BaseApiPut(string $thinkUrl = '', array $query = [], $data = [], $type = 'form'): BaseApi
    {
        return $this->amisBaseApi($thinkUrl, 'put', $query, $data, $type);
    }


    /**
     * 添加接口
     * @param string $thinkUrl thinkphp路由地址
     * @param array $query 请求参数
     * @param array $data  请求数据
     * @param mixed $type 提交类型：json   form-data    form
     * @return BaseApi
     */
    public function BaseApiPost(string $thinkUrl = '', array $query = [], $data = [], $type = 'form'): BaseApi
    {
        return $this->amisBaseApi($thinkUrl, 'post', $query, $data, $type);
    }

    /**
     * 获取接口
     * @param string $thinkUrl thinkphp路由地址
     * @param array $query 请求参数
     * @param mixed $type 提交类型：json   form-data    form
     * @return BaseApi
     */
    public function BaseApiGet(string $thinkUrl = '', array $query = [], $type = 'form'): BaseApi
    {
        return $this->amisBaseApi($thinkUrl, 'get', $query, [], $type);
    }

    /**
     * API接口
     * @param string $thinkUrl thinkphp路由地址
     * @param string $method 请求方法
     * @param array $query 请求参数
     * @param array $data  请求数据
     * @param mixed $type 提交类型：json   form-data    form
     * @return BaseApi
     */
    public function amisBaseApi(string $thinkUrl = '', string $method = 'post', array $query = [], $data = [], $type = 'form'): BaseApi
    {
        //判断$method是否有.字符串
        if (strpos($method, '.') !== false) {
            [$method, $chache] = explode('.', $method);

        } else {
            $chache = false;
        }
        $Http = BaseApi::make();
        $Http->method($method);


        if ($method !== 'get' && ! empty($data)) {
            $Http->data($data);
        }

        //获取数组$queryKeys第一个值


        // dump([$query ,$queryKeys,! empty($queryKeys) , is_string($queryKeys)]);
        if (isset($query['query']) || isset($query['options'])) {
            $query   = isset($query['query']) ? $query['query'] : [];
            $suffix  = isset($query['suffix']) ? $query['suffix'] : false;
            $domain  = isset($query['domain']) ? $query['domain'] : false;
            $options = isset($query['options']) ? $query['options'] : [];

        } elseif (isset($query[0])) {
            $query   = isset($query[0]) ? $query[0] : [];
            $suffix  = isset($query[1]) ? $query[1] : false;
            $domain  = isset($query[2]) ? $query[2] : false;
            $options = isset($query[3]) ? $query[3] : [];
        } else {

            $suffix  = false;
            $domain  = false;
            $options = [];
        }


        $Http->url($this->adminUrl($thinkUrl, $query, $suffix, $domain));
        if ($chache) {
            is_int($chache) ? $Http->cache((int) $chache) : $Http->cache(200);
        }
        foreach ($options as $method => $value) {
            $Http->$method($value);
        }
        //json   form-data    form
        $Http->dataType($type);
        return $Http;
    }
    /**
     * 生成URL
     * @param string $url
     * @param array $query
     * @param mixed $suffix
     * @param mixed $domain
     * @return string
     */
    public function adminUrl(string $url = '', array $query = [], $suffix = false, $domain = false): string
    {
        if (strpos($url, '?') !== false) {

            $queryString  = parse_url($url, PHP_URL_QUERY);
            [$url, $null] = explode('?', $url);
            parse_str($queryString, $output);
            $query = array_merge($output, $query);
        }
        if (! empty($query)) {
            $query = array_map(function ($v, $k) {
                return $k.'='.$v;
            }, $query, array_keys($query));
            // dump(  $query );
            $query = '?'.join('&', $query);
        } else {
            $query = '';
        }
        return (string) url($url, [], $suffix, $domain).$query;
    }
}